package red.mlz.userservice.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import red.mlz.common.entity.user.User;
import red.mlz.userservice.service.BaseUserService;
import red.mlz.userservice.service.UserDefine;

import java.math.BigInteger;
import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private BaseUserService baseUserService;

    @RequestMapping("/user-service/getByIds")
    public List<User> getByIds(@RequestParam(name = "id")List<BigInteger> ids){
        return baseUserService.getByIds(ids);
    }

    @RequestMapping("/user-service/loginB")
    public Boolean loginB(@RequestParam(name = "phone") String phone,
                                   @RequestParam(name = "password") String password,
                                   @RequestParam(name = "remember" , required = false) Boolean remember) {
        return remember != null && remember
            ? baseUserService.login(phone, password)
                : baseUserService.login(phone, "86" ,password,false,false,0);
    }

    @RequestMapping("/user-service/loginA")
    public Boolean loginA(@RequestParam(name = "phone") String phone,
                         @RequestParam(name = "countryCode") String countryCode,
                         @RequestParam(name = "password") String password,
                         @RequestParam(name = "noPasswd") boolean noPasswd,
                         @RequestParam(name = "remember" , required = false) Boolean remember,
                         @RequestParam(name = "lifeTime") int lifeTime) {
        return baseUserService.login(phone, countryCode, password, noPasswd,remember, lifeTime);
    }

    // 根据手机号查询用户
    @RequestMapping("/user-service/getByPhone")
    public User getByPhone(@RequestParam String phone) {
        return baseUserService.getByPhone(phone);
    }

    //注册用户
    @RequestMapping("/user-service/register")
    public BigInteger registerUser(@RequestParam(name = "name") String name,
                                       @RequestParam(name = "phone") String phone,
                                       @RequestParam(name = "gender") Integer gender,
                                       @RequestParam(name = "avatar", required = false) String avatar,
                                       @RequestParam(name = "password") String password,
                                       @RequestParam(name = "country", required = false) String country,
                                       @RequestParam(name = "province", required = false) String province,
                                       @RequestParam(name = "city", required = false) String city,
                                       @RequestParam(name = "ip") String ip){
        return baseUserService.registerUser(name,phone,gender,avatar,password,country,province,city,ip);
    }

    //刷新用户登录上下文
    @RequestMapping("/user-service/refresh")
    public void refreshUserLoginContext(@RequestParam(name = "id") BigInteger id,
                                        @RequestParam(name = "ip") String ip,
                                        @RequestParam(name = "time") int time) {
        baseUserService.refreshUserLoginContext(id,ip,time);
    }

    @RequestMapping("/user-service/extractByPhone")
    public User extractByPhone(@RequestParam(name = "phone") String phone,
                               @RequestParam(name = "countryCode") String countryCode) {
        return baseUserService.extractByPhone(phone,countryCode);
    }

    @RequestMapping("/user-service/getById")
    public User getById(@RequestParam(name = "id") BigInteger id){
        return baseUserService.getById(id);
    }

    @RequestMapping("/user-service/isGender")
    public boolean isGender(@RequestParam(name = "gender") int gender){
        return UserDefine.isGender(gender);
    }
}
